package com.ordertracker.service;

import com.ordertracker.Application;
import com.ordertracker.Session;
import com.ordertracker.dao.GeneralCrudDao;
import com.ordertracker.exception.ValidationException;
import com.ordertracker.persistent.dictionary.Position;
import com.ordertracker.persistent.dictionary.Role;
import com.ordertracker.protocol.CommandMessage;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.ws.rs.core.Response;
import javax.xml.bind.JAXBException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Description of class.
 * Created 2/7/13 1:06 PM
 *
 * @author andrey.rodin@playtech.com
 */

@Service(value = "role")
public class RoleManager extends AbstractCRUDEventHandle<Role> {

    private static Logger logger = Logger.getLogger(RoleManager.class);

    private GeneralCrudDao<Role, Long> dao;

    @Resource(name = "crudDao")
    public void setDao(GeneralCrudDao<Role, Long> dao) {
        this.dao = dao;
    }

    @Override
    protected Role unmarshal(String obj) throws Exception {
        com.ordertracker.entity.Role role = null;
        StringReader sr = new StringReader(obj);
        System.out.println("Position manager: create: "+sr);
        try {
            role = Application.getApplication().getJsonUnmarshaller().unmarshalFromJSON(sr,
                    com.ordertracker.entity.Role.class);
        } catch (JAXBException e) {
            logger.error("", e);
        }

        return transferObjectFactory.roleFromDTO(role);
    }

    @Override
    protected String getServiceName() {
        return "role";
    }

    @Override
    protected void validate(Session session, Role msg, ActionType type) throws ValidationException {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    protected Response otherAction(Session session, CommandMessage msg, Role obj, ActionType type) {
        switch (type){
            case GET_ALL_LIST:
            default:
                List<Role> roleList = dao.getAll(Role.class);
                List<com.ordertracker.entity.Role> roles = new ArrayList<com.ordertracker.entity.Role>(roleList.size());
                for (Role role : roleList) {
                    roles.add(transferObjectFactory.roleToDTO(role));
                }
                return Response.ok(roles).build();
        }
    }

    @Override
    protected boolean needToUnmarshal(ActionType type) {
        switch (type) {
            case ADD:
            case UPDATE:
                return true;
            default:
                return false;
        }
    }


    @Override
    protected com.ordertracker.entity.Role create(Session session, Role obj) {
        Role role = dao.saveEntity(obj);
        return transferObjectFactory.roleToDTO(role);
    }

    @Override
    protected Object receive(CommandMessage msg) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    protected Object delete(Session session, Role obj) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    protected Object update(Session session, Role obj) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
